package ext.vpde.cadvaulting.workflow;

import java.io.*;
import java.util.*;
import com.infoengine.SAK.IeService;
import com.infoengine.SAK.Task;
import com.infoengine.au.NamingService;
import com.infoengine.au.NamingServiceException;
import com.infoengine.util.IEException;

import ext.vpde.cadvaulting.migration.LoadUtil;
import ext.vpde.cadvaulting.util.WINDSQL;
import wt.util.WTProperties;

public class StartWorkflow {

	public static String USERID        = null;
	public static String USERPW        = null;
	public static String ADAPTER       = null;
	public static String RESOURCE      = null;
	public static String NAMINGSERVICE = null;
	
	public static void main ( String [] args ) throws Exception {
		
		if (args.length != 3) {
			System.out.println("\nUsage: java.ext.productcenter.LoadContent loadfile adminid adminpw");
		} else {
			
			USERID     = args[1];
			USERPW     = args[2];
			
			WTProperties wtProps = WTProperties.getLocalProperties();
		    ADAPTER       = wtProps.getProperty("wt.federation.ie.VMName");
		    RESOURCE      = wtProps.getProperty("wt.federation.ie.propertyResource");
		    NAMINGSERVICE = wtProps.getProperty("wt.federation.ie.namingService");
			
			System.out.println("BEGIN START WORKFLOW: " + Calendar.getInstance().getTime());
			WINDSQL.openConnection();
            readEntries(args[0]);
            WINDSQL.closeConnection();
        	System.out.println("--END START WORKFLOW: " + Calendar.getInstance().getTime());

		}
    }
	
	private static void readEntries(String inputFile) throws IOException, Exception {
		BufferedReader file = new BufferedReader(new FileReader(inputFile));
		String s = file.readLine();
		int lineNumber = -1;
		while (s!= null && s.length() > 0) {				
			StringTokenizer strTok = new StringTokenizer(s,"|");
			String docName = strTok.nextToken();
			String docVer  = strTok.nextToken();
			String docIter = strTok.nextToken();
			                 strTok.nextToken();
			                 strTok.nextToken();
			String fPath   = (strTok.hasMoreTokens()) ? strTok.nextToken() : null;
			if (fPath == null) {
				System.out.println("POSSIBLE NEWLINE IN TITLE: " + docName + " " + docVer + "." + docIter);
			} else {
				if (!fPath.equals("none")) {
					if (WINDSQL.documentExists(docName,docVer,docIter)) {
						String obid = "OR:wt.epm.EPMDocument:" + WINDSQL.getDocument(docName,docVer,docIter);
						createProcess(obid,docName,docVer,docIter);
					}
				}
			}
			s = file.readLine();
			LoadUtil.printProgress(++lineNumber);
		}
	}
	
	public static void createProcess(String obid, String docName, String docVer, String docIter) throws IOException, NamingServiceException, IEException {
			Task t = new Task("ext/vpde/cadvaulting/StartWorkflow.xml");
			NamingService.setVMName ("remote");
			NamingService.newInstance (NAMINGSERVICE,RESOURCE,null);
			IeService ie = new IeService();
			ie.setEnableExceptions ( true );
			ie.setCredentials(USERID, USERPW);
			Vector processors=new Vector();
			processors.add(ADAPTER);
			t.setProcessors(processors);
			t.setUsername(USERID);
			t.setService (ie);
			t.addParam("OBID",obid);
			t.addParam("PROCESS_NAME","Release " + docName + ", " + docVer + "." + docIter);
			t.addParam("USERID",USERID);
			t.addParam("USERPW",USERPW);
			t.invoke();
	}
}